import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:archime/houses.dart';

typedef ShowCallback = Function(bool flag);
typedef DisplayCallback = Function(String text);
Map<String, double> _indexs = new Map();
ScrollController _scrollController = new ScrollController();
GlobalKey _listKey = new GlobalKey();
double booksCount = 0;

class Conversation extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ConversationState();
  }
}

class ConversationState extends State<Conversation> {
  bool isShow = false;
  String displayText = "";
  void _setShow(bool flag) {
    setState(() {
      isShow = flag;
    });
  }

  void _setDisplayText(String text) {
    setState(() {
      displayText = text;
      if (_indexs.containsKey(text)) {
        var o = _indexs[text].toDouble();
        var height = _listKey.currentContext.size.height;
        o = booksCount - o < height ? booksCount - height : o;
        _scrollController.jumpTo(o);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: PreferredSize(
            child: AppBar(
              title: new Text("烟火"),
              actions: <Widget>[
                new IconButton(
                  icon: new Icon(Icons.search),
                  onPressed: () {},
                ),
                new IconButton(
                  icon: new Icon(Icons.record_voice_over),
                  onPressed: () {},
                ),
              ],
              elevation: 0.0,
            ),
            preferredSize: Size.fromHeight(ScreenUtil.screenHeight * 0.02),
        ),
        body: Stack(
          children: <Widget>[
            Dialogs(),
            Align(
              child: Offstage(
                offstage: !isShow,
                child: Container(
                  width: ScreenUtil().setWidth(200),
                  height: ScreenUtil().setHeight(160),
                  decoration: BoxDecoration(
                    color: Colors.grey,
                    borderRadius: BorderRadius.circular(6.0),
                  ),
                  child: Center(
                    child: Text(
                      displayText,
                      style: TextStyle(
                          fontSize: ScreenUtil().setSp(100),
                          color: Colors.white),
                    ),
                  ),
                ),
              ),
              alignment: Alignment.center,
            )
          ],
        ));
  }
}

class Dialogs extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return DialogsState();
  }
}

class DialogsState extends State<Dialogs> {
  var list = [
        {"Name": "爱妃0", "Number": "a1230", "avatar": "images/avatar0.png"},
        {"Name": "爱妃1", "Number": "a1231", "avatar": "images/avatar1.png"},
        {"Name": "爱妃2", "Number": "a1232", "avatar": "images/avatar1.png"},
        {"Name": "爱妃3", "Number": "a1233", "avatar": "images/avatar1.png"},
        {"Name": "爱妃4", "Number": "a1234", "avatar": "images/avatar0.png"},
        {"Name": "爱妃5", "Number": "a1234", "avatar": "images/avatar0.png"},
        {"Name": "爱妃6", "Number": "a1234", "avatar": "images/avatar0.png"},
        {"Name": "爱妃7", "Number": "a1234", "avatar": "images/avatar0.png"},
        {"Name": "爱妃8", "Number": "a1234", "avatar": "images/avatar0.png"},
  ];
  double dyCount = 0;
  List<Widget> creatList() {
    List<Widget> conversionList = [];
    dyCount = 0;
    booksCount = 0;
    list.forEach((item) {
      conversionList.add(Container(
              child: new FlatButton(
                onPressed: /**_readArticle(position)*/ () {
                  Widget path = Houses(
                    title: item["Name"],
                  );
                  Navigator.push(
                      context, new MaterialPageRoute(builder: (context) => path));
                },
                child: ListTile(
                  leading: Stack(
                    children: <Widget>[
                        Image.asset(
                          item["avatar"],
                          width: ScreenUtil().setWidth(90),
                        ),
                    ],
                  ),
                  title: Text(item["Name"]),
                )
              ),
            height: ScreenUtil().setHeight(112),
          )
        );
    });
    return conversionList;
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      key: _listKey,
      controller: _scrollController,
      children:
          ListTile.divideTiles(context: context, tiles: creatList()).toList(),
    );
  }
}
